package com.huatai.vba.execute.action;

import java.text.SimpleDateFormat;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huatai.vba.common.exception.BusinessException;
import com.huatai.vba.common.utils.ListUtils;
import com.huatai.vba.dal.jdbc.DBUtils;
import com.huatai.vba.dal.model.VbaFileAction;
import com.huatai.vba.properties.Dbconfig;
import com.huatai.vba.service.VbaExtendsService;

@Service("compareActionExecutor")
public class CompareActionExecutor implements ActionExecutor {

	private static final Logger logger = LoggerFactory.getLogger(CompareActionExecutor.class);

	@Autowired
	private VbaExtendsService vbaExtendsService;

	private final String ta_user = "TATHREE";

	private final String gz_user = "HTZCQRY";

	@Override
	public void execute(VbaFileAction action) throws BusinessException {
		String yyyyMMdd = new SimpleDateFormat("yyyyMMdd").format(new Date());
		// 删除原来的数据
		String deleteSql = "delete from comparedata where d_date = '" + yyyyMMdd + "'";
		vbaExtendsService.executeSql(deleteSql);
		String taSql = " select to_char(d_date, 'yyyymmdd') d_date,  upper(c_fundcode) ta_c_fundcode,"
				+ "  f_lastshares ta_f_lastshares,  f_lastasset ta_f_lastasset,  f_netvalue ta_f_netvalue,"
				+ "  f_totalnetvalue ta_f_totalnetvalue  from tfundday t where to_char(d_date, 'yyyymmdd') = '"
				+ yyyyMMdd + "'  and f_lastshares > 0  order by c_fundcode asc  ";

		String gzSql = " select to_char(fdate, 'yyyymmdd') gz_d_date, a.fsetname gz_c_fundname, upper(a.fsetid) gz_c_fundcode,"
				+ " sum(decode(fkmbm, '601实收基金', fzqsl, '601实收资本', fzqsl, 0)) gz_f_lastshares,"
				+ " sum(decode(fkmbm, '701基金资产净值：', fzqsz, 0)) gz_f_lastasset,"
				+ " sum(decode(fkmbm, '702基金单位净值：', fzqsz, 0)) gz_f_netvalue,"
				+ " sum(decode(fkmbm, '905累计单位净值：', fkmmc, 0)) gz_f_totalnetvalue"
				+ "   from view_gzb a, lsetlist b where to_char(fdate,'yyyymmdd') = '" + yyyyMMdd
				+ "'   and b.fyear = to_number(to_char(sysdate, 'yyyy'))   "
				+ "	   and a.fsetid = b.fsetid   and a.fsetname = b.fsetname  "
				+ " group by a.fsetid, a.fsetname, fdate order by a.fsetid asc ";
		try {
			List<Map<String, Object>> taData = DBUtils.executeQuery(Dbconfig.FATADB, ta_user,
					Dbconfig.getPasswordByUserId(ta_user), taSql);
			String taColumns = "d_date,c_fundcode,ta_f_netvalue,ta_f_lastshares,ta_f_lastasset,ta_f_totalnetvalue";
			if (ListUtils.isEmpty(taData)) {
				logger.info("TA没有数据，事件异常结束");
				throw new BusinessException("TA没有数据，事件异常结束");
			}

			for (Map<String, Object> map : taData) {
				StringBuffer insertSql = new StringBuffer("insert into comparedata( " + taColumns + ")values (");
				insertSql.append("'").append(map.get("d_date".toUpperCase())).append("',");
				insertSql.append("'").append(map.get("ta_c_fundcode".toUpperCase())).append("',");
				insertSql.append(map.get("ta_f_netvalue".toUpperCase())).append(",");
				insertSql.append(map.get("ta_f_lastshares".toUpperCase())).append(",");
				insertSql.append(map.get("ta_f_lastasset".toUpperCase())).append(",");
				insertSql.append(map.get("ta_f_totalnetvalue".toUpperCase()));
				insertSql.append(")");
				vbaExtendsService.executeSql(insertSql.toString());
			}

			List<Map<String, Object>> gzData = DBUtils.executeQuery(Dbconfig.FATADB, gz_user,
					Dbconfig.getPasswordByUserId(gz_user), gzSql);
			for (Map<String, Object> map : gzData) {
				StringBuffer updateSql = new StringBuffer("update comparedata ");
				updateSql.append(" set c_fundname = '").append(map.get("gz_c_fundname".toUpperCase())).append("'");
				updateSql.append(",gz_f_netvalue = ").append(map.get("gz_f_netvalue".toUpperCase()));
				updateSql.append(",gz_f_lastshares = ").append(map.get("gz_f_lastshares".toUpperCase()));
				updateSql.append(",gz_f_lastasset = ").append(map.get("gz_f_lastasset".toUpperCase()));
				updateSql.append(",gz_f_totalnetvalue = ").append(map.get("gz_f_totalnetvalue".toUpperCase()));
				updateSql.append(" where d_date='").append(map.get("gz_d_date".toUpperCase())).append("'");
				updateSql.append(" and c_fundcode='").append(map.get("gz_c_fundcode".toUpperCase())).append("'");
				Integer num = vbaExtendsService.executeSql(updateSql.toString());

			}
		} catch (Exception e) {
			logger.error("系统异常", e);
			throw new BusinessException(e.getMessage());
		}

	}

}
